package com.example.demo.controller;

import com.example.demo.entity.DictItem;
import com.example.demo.entity.ProductSupplier;
import com.example.demo.exception.IndexException;
import com.example.demo.model.SupplierModel;
import com.example.demo.request.Supplier.AddRequest;
import com.example.demo.request.Supplier.ListRequest;
import com.example.demo.response.Response;
import com.example.demo.response.supplier.DetailFormat;
import com.example.demo.services.SupplierServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class SupplierController {
    @Autowired SupplierModel SupplierModel;
    @RequestMapping("/supplier/list")
    @ResponseBody
    public Response suppliers(ListRequest listRequest) {
        return Response.data(SupplierModel.getList(listRequest));
    }

    @Autowired SupplierServices SupplierServices;
    @RequestMapping("/supplier/detail")
    @ResponseBody
    public Response detail(@RequestParam("id") int id) {
        ProductSupplier ProductSupplier = SupplierServices.getSupplier(id);
        List<DictItem> insure_kind_list = SupplierServices.getDictItemByIds(ProductSupplier.insure_kind_list);
        List<DictItem> agreement = SupplierServices.getDictItemByIds(ProductSupplier.agreement);
        return Response.data(new DetailFormat(ProductSupplier, insure_kind_list,agreement));
    }


    @RequestMapping("/supplier/add")
    @ResponseBody
    public Response add(@Validated AddRequest AddRequest, BindingResult bindResult) {
        if (bindResult.hasErrors()) {
            throw new IndexException(402,bindResult.getFieldError().getDefaultMessage());
        }
        return Response.success(200,"添加成功");
    }

}
